﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace VNSoft.Administrator
{
    public partial class Hotro : System.Web.UI.Page
    {
        clsXuLy ex = new clsXuLy();
        SqlParameter[] p;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancel_Click(sender, e);
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text.Trim().Equals(""))
                gstGetMess("Nhập họ tên nhân viên hỗ trợ", "");
            else
            {
                try
                {
                    p = new SqlParameter[7];
                    p[0] = new SqlParameter("@Name", txtHoTen.Text.Trim());
                    p[0].SqlDbType = SqlDbType.NVarChar;
                    p[1] = new SqlParameter("@Yahoo", txtYahoo.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@Skype", txtSkype.Text.Trim());
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@Zoom", txtMoTa.Text.Trim());
                    p[3].SqlDbType = SqlDbType.NVarChar;
                    p[4] = new SqlParameter("@Active", ckHienThi.Checked ? 1 : 0);
                    p[4].SqlDbType = SqlDbType.Int;
                    p[5] = new SqlParameter("@Mobile", txtMobile.Text.Trim());
                    p[5].SqlDbType = SqlDbType.NVarChar;
                    p[6] = new SqlParameter("@ID", Guid.NewGuid());
                    p[6].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_Support_Add", p);
                    btnCancel_Click(sender, e);
                    gstGetMess("Thêm thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");
                }
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text.Trim().Equals(""))
                gstGetMess("Nhập họ tên nhân viên hỗ trợ", "");
            else
            {
                try
                {
                    Guid giD = new Guid(hdID.Value);
                    p = new SqlParameter[7];
                    p[0] = new SqlParameter("@Name", txtHoTen.Text.Trim());
                    p[0].SqlDbType = SqlDbType.NVarChar;
                    p[1] = new SqlParameter("@Yahoo", txtYahoo.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@Skype", txtSkype.Text.Trim());
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@Zoom", txtMoTa.Text.Trim());
                    p[3].SqlDbType = SqlDbType.NVarChar;
                    p[4] = new SqlParameter("@Active", ckHienThi.Checked ? 1 : 0);
                    p[4].SqlDbType = SqlDbType.Int;
                    p[5] = new SqlParameter("@ID", giD);
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[6] = new SqlParameter("@Mobile", txtMobile.Text.Trim());
                    p[6].SqlDbType = SqlDbType.NVarChar;
                    ex.ExecuteNonQuerry("sp_Support_Update", p);
                    btnCancel_Click(sender, e);
                    gstGetMess("Cập nhật thành công", "");
                }
                catch (Exception ax)
                {
                    gstGetMess("Lỗi: " + ax.Message, "");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtHoTen.Text = "";
            txtYahoo.Text = "";
            txtSkype.Text = "";
            txtMoTa.Text = "";
            txtMobile.Text = "";
            ckHienThi.Checked = true;
            gvHoTro.DataSource = ex.GetData("sp_Support_List");
            gvHoTro.DataBind();

            btnAdd.Visible = true;
            btnUpdate.Visible = false;
        }

        protected void gvHoTro_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();
            hdID.Value = ID;
            Guid gID = new Guid(hdID.Value);
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@ID", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            if (e.CommandName.Equals("Select"))
            {
                DataTable dt = ex.GetData("sp_Support_ListByID", p);
                if (dt.Rows.Count > 0)
                {
                    txtHoTen.Text = dt.Rows[0]["Name"].ToString();
                    txtYahoo.Text = dt.Rows[0]["NickYahoo"].ToString();
                    txtSkype.Text = dt.Rows[0]["NickSkype"].ToString();
                    ckHienThi.Checked = dt.Rows[0]["Active"].ToString().Trim().Equals("1") ? true : false;
                    txtMoTa.Text = dt.Rows[0]["Zoom"].ToString();
                    txtMobile.Text = dt.Rows[0]["Mobile"].ToString();

                }

                btnUpdate.Visible = true;
                btnAdd.Visible = false;
            }
            else
                if (e.CommandName.Equals("Del"))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_Support_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Lỗi: " + ax.Message, "");
                    }
                }
        }
        private void gstGetMess(string gstMess, string gstLink)
        {
            if (gstLink == "")
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
            else
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

        }
    }
}